
const fs = require('fs');
var fileList = fs.readdirSync('C:\\Users\\QI\\Desktop\\tilesImg')


let obj = {}
let points = []
let aceTiles = ['p', 'g', 's', 'g']
let desertTiles = ['p', 'g', 's', 'g']
for (let x = -400; x <= 400; x++) {
    for (let y = -400; y <= 400; y++) {
        createImg(x, y)

    }
}

function random() {
    return Math.floor(Math.random() * 4)
}

function addOne(num) {
    let a = num + 1
    if (a > 400) {
        a = -400
    }
    return a
}

function reduceOne(num) {
    let a = num - 1
    if (a < -400) {
        a = 400
    }
    return a
}

function createImg(x, y) {


    let up = 0
    let right = 0
    let down = 0
    let left = 0

    let upPos = obj[x + ':' + addOne(y)]
    let rightPos = obj[addOne(x) + ':' + y]
    let downPos = obj[x + ':' + reduceOne(y)]
    let leftPos = obj[reduceOne(x) + ':' + y]

    if (upPos) {
        up = upPos[3]
        right = upPos[2]
    }
    if (rightPos) {
        right = rightPos[0]
        down = rightPos[3]
    }
    if (downPos) {
        down = downPos[1]
        left = downPos[0]
    }
    if (leftPos) {
        left = leftPos[2]
        up = leftPos[1]
    }

    if (x == -400 && y == -400) {
        up = 'g'
        right = 'g'
        down = 'g'
        left = 'g'
    }

    let isDesert = [up, right, down, left].includes('d')
    let isAce = [up, right, down, left].includes('a')



    if (!isDesert && !isAce) {
        if (Math.random() > 0.5) {
            points = desertTiles
        } else {
            points = aceTiles
        }
    }


    if (isDesert) {
        points = desertTiles
    }

    if (isAce) {
        points = aceTiles
    }

    if (!up) {

        up = getRandomPoint(up, right, down, left)

    }
    if (!right) {
        right = getRandomPoint(up, right, down, left)
    }
    if (!down) {
        down = getRandomPoint(up, right, down, left)
    }
    if (!left) {
        left = getRandomPoint(up, right, down, left)
    }

    let exit = isExitImg(up, right, down, left)
    obj[x + ':' + y] = [up, right, down, left]

    return exit
}

function isExitImg(up, right, down, left) {
    let flag = fileList.includes(`${up}_${right}_${down}_${left}.png`)
    if (flag) {
        // console.log(`${up}_${right}_${down}_${left}.png，存在`)
    } else {
        console.log(`${up}_${right}_${down}_${left}.png，不存在`)
    }

    return flag
}

function getRandomPoint(up, right, down, left) {
    return points[random()]
    if (Math.random() > 0.95) {
        return points[random()]
    } else {
        if (left) return left
        if (down) return down
        if (up) return up
        if (right) return right
    }


}


function getZoneIdByXy(x, y) {
    let square = 400
    if (x > square || x < -square) {
        x = (x > square) ? (x - (square * 2 + 1)) : (x + (square * 2 + 1));
    }
    if (y > square || y < -square) {
        y = (y > square) ? (y - (square * 2 + 1)) : (y + (square * 2 + 1));
    }
    return (((x + square) * (square * 2 + 1)) + (y + (square + 1)));
}

function getXyByZoneId(zoneId) {
    let radius = 400
    let x = ((zoneId / (radius * 2 + 1)) | 0) - radius;
    let y = (zoneId % (radius * 2 + 1)) - (radius + 1);
    if (y == -(radius + 1)) {
        y = radius;
        x--;
    }

    if (x > radius || x < - radius) {
        x = (x > radius) ? (x - (radius * 2 + 1)) : (x + (radius * 2 + 1));
    }
    if (y > radius || y < - radius) {
        y = (y > radius) ? (y - (radius * 2 + 1)) : (y + (radius * 2 + 1));
    }
    return [x, y];
}



// try {
//     fs.mkdirSync('tilesData');
//     console.log('文件夹创建成功!');
// } catch (err) {
//     console.error(err);
// }
// for (let key in obj) {
//     let xy = key.split(':')
//     let x = parseInt(xy[0])
//     let y = parseInt(xy[1])

//     let pos = getZoneIdByXy(x, y)
//     console.log('tilesData/' + pos + '.txt', obj[key].join(''))
//     fs.writeFileSync('tilesData/' + pos + '.txt', obj[key].join(''));
// }
// console.log("成功创建了地图数据");


let txt = ''
for(let key in obj){
    let xy = key.split(':')
    let x = parseInt(xy[0])
    let y = parseInt(xy[1])
    // txt += getZoneIdByXy(x, y) + ':' + obj[key].join('') + ';'
    txt +=   obj[key].join('') + ';'
}
 
// 将JSON字符串写入文件
fs.writeFileSync('tiles.txt', txt);
console.log("成功创建了名为'data.json'的JSON文件");